package com.pic.mgt.syushin.initials;

import java.io.File;
import java.util.List;

import com.pic.mgt.initials.InitialBean;
import com.pic.mgt.initials.InitialOperationException;
import com.pic.mgt.manager.business.BasicBusinessInfoBean;
import com.pic.mgt.manager.business.BusinessManager;
import com.pic.mgt.monitor.UnitMonitor;
import com.pic.mgt.monitor.UnitMonitorException;
import com.pic.mgt.syushin.action.SyushinBusinessApMgtAction;

// (送多方新增)
public class SyushinAddMessageFoldersInitialBean implements InitialBean {
	
	private SyushinBusinessApMgtAction syushinBusinessApMgtAction;
	private UnitMonitor messageUnitMonitor;
	private BusinessManager businessManager;

	@Override
	public void initialize() throws InitialOperationException {
		// 要新增訊息目錄的監控時，必須要先完成各業務的 AP Info資訊 
		List<BasicBusinessInfoBean> effBasicBusinessInfoBeanList =
					this.businessManager.getEffectiveBasicBusinessInfoBeanList();
		for (BasicBusinessInfoBean effBasicBusinessInfoBean: effBasicBusinessInfoBeanList) {
			this.syushinBusinessApMgtAction.fillTheApFolderIntoBasicBusinessInfoBean(effBasicBusinessInfoBean);
		}
		
		// 開始新增
		List<File> syushinApFolderList = this.syushinBusinessApMgtAction.getAllApFolders();
		for (File syushinApFolder : syushinApFolderList) {
			try {
				this.messageUnitMonitor.addMonitorPath(syushinApFolder.getAbsolutePath());
			} catch (UnitMonitorException e) {
				throw new InitialOperationException(
				"Initializing failed: Adding syushin message folder monitor failed: " + e.getMessage());
			}
		}
	}
	
	// --- getter and setter ---
	
	public void setSyushinBusinessApMgtAction(
			SyushinBusinessApMgtAction syushinBusinessApMgtAction) {
		this.syushinBusinessApMgtAction = syushinBusinessApMgtAction;
	}

	public void setMessageUnitMonitor(UnitMonitor messageUnitMonitor) {
		this.messageUnitMonitor = messageUnitMonitor;
	}

	public void setBusinessManager(BusinessManager businessManager) {
		this.businessManager = businessManager;
	}

}
